package com.lvyq.myplayer.ui.fragment.singer

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.singer.SingerVideoAdapter
import com.lvyq.myplayer.model.singer.SingerSongBean
import com.lvyq.myplayer.model.singer.SingerVideoBean
import com.lvyq.myplayer.model.singer.SingerVideoData
import com.lvyq.myplayer.parcelable.MvPagerBeansData
import com.lvyq.myplayer.ui.activity.AudioPlayerActivity
import com.lvyq.myplayer.ui.activity.VideoPlayerActivity
import com.lvyq.myplayer.util.NetworkUtils
import com.lvyq.myplayer.util.ThreadUtil
import com.lvyq.myplayer.util.URLProviderUtils
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.jetbrains.anko.runOnUiThread
import org.jetbrains.anko.toast
import java.io.IOException
import kotlin.time.Duration

/**
 * 文件描述：SingerVideoPagerFragment
 * 作者：lvyq
 * 创建时间：2025-04-25
 * 修改时间：2025-04-25
 */
class SingerVideoPagerFragment(var singerName:String):  Fragment() {


    lateinit var singerAlbumView: RecyclerView


    val singerVideoAdapter by lazy { getAdapter() }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return initView()
    }

    private fun initView(): View? {
        var view  = View.inflate(context, R.layout.fragment_singer_album,null)
        singerAlbumView=view.findViewById(R.id.singerAlbumView)

        singerAlbumView.layoutManager = GridLayoutManager(context, 2)
        //适配
        singerAlbumView.adapter=singerVideoAdapter
        initData()
        return view

    }

    private fun initData() {
        //不分页
        val url = URLProviderUtils.getSingerVideoList(
            singerName)
        NetworkUtils.get(
            url = url,
            responseClass = SingerVideoBean::class.java,
            successCallback = { pageData ->
                singerVideoAdapter.initData(
                    ArrayList(pageData.rows)
                )
            },
            errorCallback = { errorMsg ->
                Toast.makeText(context, "加载失败：$errorMsg", Toast.LENGTH_SHORT).show()
                Log.e("SingerSongPagerFragment", "加载失败：$errorMsg")
            }
        )
    }

    fun getAdapter(): SingerVideoAdapter {
        return SingerVideoAdapter(context,object :
            SingerVideoAdapter.OnItemClickListener{
            override fun onItemClick(position: Int, video: SingerVideoData) {

                val client = OkHttpClient()
                val request = Request.Builder()
                    .url(URLProviderUtils.reUpdVideo(video.id))
                    .build()

                // 改为异步请求
                client.newCall(request).enqueue(object : Callback {
                    override fun onResponse(call: Call, response: Response) {
                        val newVideoUrl = response.body?.string()
                        // 在主线程更新UI
                        Handler(Looper.getMainLooper()).post {
                                // 点击跳转播放页面
                                var intent = Intent(context, VideoPlayerActivity::class.java)
                                val data = MvPagerBeansData(video.id,"TJ",video.title,video.singer,newVideoUrl,video.img,video.singer,"")
                                intent.putExtra("data",data)
                                startActivity(intent)
                        }
                    }

                    override fun onFailure(call: Call, e: IOException) {
                        // 错误处理
                        Log.e("SingerVideoPagerFragment", "Request failed: ${e.message}")
                        /* nextMusicPlay()*/
                    }
                })
            }
        })
    }



}